<?php
// FIXED REVIEW Trung Nguyen 2009-22-07
// Wrong package name

/**
 * This class supports actions for bed management, ex change beds..
 *
 * @package core.includes.BedManagement
 * @author pktquynh
 * @version $Id:$
 * @copyright 2009 LHV Company
 */

class BedManagement {
	/**
     * This method is called when changing department or pay money to come out of hospital.<br>
     * Finalize bed status of patient in some department like this:<br>
     * search table 'patient_bed' by criteria ('patient_id', 'current=true'),<br> 
     * update 'current=false', end_date=TODAY<br>
     * @param int $patientId the id of patient
     */
    public function finalizeBedForOutputDept($patientId) {
    	$sql = "UPDATE patient_bed SET current = 0, end_date = NOW() "
    	     . " WHERE patient_id = '$patientId'"
    	     . " AND current=1";
    	$patBed = Celini::newORDO('PatientBed');     
        $patBed->_execute($sql);
    }
	
    /**
     * When patient enters into some department, he hasn't be assigned bed yet or no bed of this department is   
     * available. It must assign default bed (bed_id = 0) into table 'patient_bed' (to pay money when 
     * coming out of hospital).
     * @param int $patientId the id of patient
     * @param int $deptId the id of department
     * @param int $ticketId the id of ticket 
     */
    public function addDefaultBedForInputDept($patientId, $deptId, $ticketId) {
    	// get default bed
    	$defBed = Celini::newORDO("Bed", 1);
    	    	
        $patBedId = $this->changeBed($patientId, $deptId, $ticketId, $defBed);
    }
    
    /**
     * Get information of current bed of the specific patient.
     * 
     * @param int $patientId The id of patient
     * @return false|array return false if the patient don't stay in any bed
     */
    public function getCurrentBedOfPatient($patientId) {
    	$patBed = Celini::newORDO('PatientBed');
    	return $patBed->getCurrentBedOfPatient($patientId);
    }
    
    /**
     * Change the current bed of patient from in_used status to unused status.
     * @param $patientId The id of patient
     * @param $deptId The id of department
     */
    public function changeBedStatus($patientId, $deptId) {
    	$patBed = Celini::newORDO('PatientBed');
        return $patBed->changeBedStatus($patientId, $deptId);
    }
    
    /**
     * Change a specific bed for some patient at department.
     * @param $patientId The id of patient
     * @param $deptId The id of department
     * @param $ticketId The id of ticket
     * @param $bed the bed object
     */
    public function changeBed($patientId, $deptId, $ticketId, $bed) {
    	$patBed = Celini::newORDO('PatientBed');
        return $patBed->changeBed($patientId, $deptId, $ticketId, $bed);
    }
    
    /**
     * Get all rooms which has at least one bed in the specific department.
     * 
     * @param int $deptId The id of department
     */
    public function getRoomsAtLeastBed($deptId) {
    	$bed = Celini::newORDO('Bed');
    	return $bed->getRoomsAtLeastBed($deptId);
    }
    
    /**
     * Get all specific rooms with information of beds.
     * @param $roomIds The list of room id
     */
    public function getRoomsWithAvailableBeds($roomIds) {
    	$bed = Celini::newORDO('Bed');
    	return $bed->getRoomsWithAvailableBeds($roomIds);
    }
    
    /**
     * Get available beds of the specific room.
     */
    public function getAvailableBedsOfRoom($roomId) {
    	$bed = Celini::newORDO('Bed');
        return $bed->getAvailableBedsOfRoom($roomId);
    }
    
    /**
     * Get bed information and current number of patients for all beds of a specific room.
     * @param int $roomId The id of room
     * @return array contains (bed_id, code, max_patients and patients) of beds
     */
    public function getNumberOfPatientsForBed($roomId) {
    	$bed = Celini::newORDO('Bed');
        return $bed->getNumberOfPatientsForBed($roomId);
    }
}

?>